package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.audiobuddy.serializables.Song;
import com.audiobuddy.serializables.SongCommand;


public class ServerMainJFrame extends JFrame implements ActionListener {

	private GuiController gc;
	
	
	
	//-----------------------------------JBUTTONS--------------------------------
	private JButton btnPlay;
	private JButton btnPause;
	private JButton btnStop;
	private JButton btnNext;
	private JButton btnPrev;
	private JButton btnMulticastPlaylist;


	//------------------------------------JLABELS---------------------------------
	private JLabel lblCurrentSong;


	//------------------------------------JPANELS--------------------------------------
	private JPanel mainPanel;



	private JTable songTable;



	private JScrollPane songScrollPane;


	
	public ServerMainJFrame(GuiController guiController) {
		this.gc = guiController;
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				initGui();
			}
		});
	}
	
	//-------------------------------------------------INITGUI----------------------------------
	protected void initGui() {
		createFrame();
		addContentPanel();
		addControls();
		addSongTable();
		addBtnMulticastPlaylist();
		
	}
	
	private void addBtnMulticastPlaylist() {
		//Enkel gebruikt tijdens het testen, dus nu invisible
		btnMulticastPlaylist = new JButton("Multicast Playlist");
		btnMulticastPlaylist.addActionListener(this);
		mainPanel.add(btnMulticastPlaylist, BorderLayout.SOUTH);
		btnMulticastPlaylist.setVisible(false);
		
	}

	private void addSongTable() {
		songTable = new JTable(gc.getSongTableModel());
		songScrollPane = new JScrollPane(songTable);
		mainPanel.add(songScrollPane, BorderLayout.CENTER);
		
	}


	private void addControls() {
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new FlowLayout());
		controlPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		controlPanel.setSize(700,100);
		// Volume slider
		final JSlider volume = new JSlider(JSlider.HORIZONTAL,
				0, 100,100);
		volume.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				gc.setVolume(volume.getValue());
				
			}
		});
		
		
		// Buttons
		btnPlay = new JButton("Play");
		btnPause = new JButton("Pause");
		btnStop = new JButton ("Stop");
		btnNext = new JButton("Next");
		btnPrev = new JButton("Previous");
		//controlPanel.add(volume);
		controlPanel.add(btnPlay);
		//controlPanel.add(btnPause);
		controlPanel.add(btnStop);
		controlPanel.add(btnNext);
		controlPanel.add(btnPrev);
		btnPlay.addActionListener(this);
		btnPause.addActionListener(this);
		btnStop.addActionListener(this);
		btnNext.addActionListener(this);
		btnPrev.addActionListener(this);
		//This class is also an ActionListener (implements it) so we don't have to
		//create a new actionListener for each Button.
		//This saves memory usage.
		//All the code should be written in the actionPerformed() method below
		
		// Currently playing
		JLabel playing = new JLabel("Currently playing:");
		controlPanel.add(playing);
		
		lblCurrentSong = new JLabel("");
		controlPanel.add(lblCurrentSong);
		
		mainPanel.add(controlPanel, BorderLayout.NORTH);
	}

	private void addContentPanel() {
		mainPanel = new JPanel(new BorderLayout());
		mainPanel.setSize(new Dimension(800, 600));
		this.setContentPane(mainPanel);
		
	}

	private void createFrame() {
		this.setTitle("AudioBuddy Server 0.1");
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(800, 600);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		BufferedImage image = null;
		try {
			image = ImageIO.read(this.getClass().getResource("audiobuddy1.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setIconImage(image);
		
	}
	
	
	
	
	
	
	
	
	
	
	//--------------------------------------------All the button actions in here-----------------------------------------------------------

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source == btnMulticastPlaylist){
			gc.multicastPlaylist();
		}
		
		if(source == btnPlay){
			if(songTable.getSelectedRow()!=-1){//betekent geen rij geselecteerd
				gc.sendCommand(new SongCommand(SongCommand.STOP_BROADCASTING,0));
				
				gc.sendCommand(new SongCommand(SongCommand.STOP,0));
				try {
					Thread.sleep(750);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				gc.sendCommand(new SongCommand(SongCommand.PLAY, songTable.getSelectedRow()));
			}
		}
		
		if(source == btnPause){
			
		}
		
		if(source == btnNext){
			gc.sendCommand(new SongCommand(SongCommand.STOP_BROADCASTING,0));
			gc.sendCommand(new SongCommand(SongCommand.STOP,0));
			try {
				Thread.sleep(750);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			gc.sendCommand(new SongCommand(SongCommand.NEXT, 0));
		}
		
		if(source == btnStop){
			gc.sendCommand(new SongCommand(SongCommand.STOP_BROADCASTING,0));
			gc.sendCommand(new SongCommand(SongCommand.STOP,0));
		}
		
		if(source == btnPrev){
			gc.sendCommand(new SongCommand(SongCommand.STOP_BROADCASTING,0));
			
			gc.sendCommand(new SongCommand(SongCommand.STOP,0));
			try {
				Thread.sleep(750);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			gc.sendCommand(new SongCommand(SongCommand.PREVIOUS, 0));
		}
		
	}

	public void setCurrentSong(Song currentSong) {
		lblCurrentSong.setText(currentSong.getArtist() + " - " + currentSong.getTitle());
		
	}

}
